package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.OrderDetail;
import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.OrderDetailMapper;
import cn.wolfcode.trip.base.mapper.OrderMapper;
import cn.wolfcode.trip.base.mapper.SignInMapper;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.service.IOrderService;
import cn.wolfcode.trip.base.util.JasonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements IOrderService {

    private final OrderMapper orderMapper;
    private final OrderDetailMapper orderDetailMapper;
    private final SignInMapper signInMapper;

    @Autowired
    public OrderServiceImpl(OrderMapper orderMapper, OrderDetailMapper orderDetailMapper, SignInMapper signInMapper) {
        this.orderMapper = orderMapper;
        this.orderDetailMapper = orderDetailMapper;
        this.signInMapper = signInMapper;
    }

    /**
     * 分页查询订单
     *
     * @param qo 条件
     * @return 商品列表
     */
    @Override
    public PageInfo<Order> queryForList(OrderQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Order> list = orderMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    /**
     * 新增或保存订单
     */
    @Override
    public void saveOrUpdate(Long userId, Long goodsId, Integer totalIntegral, Integer state) {
        Order order = new Order();
        User user = new User();
        OrderDetail detail = new OrderDetail();
        // 立即兑换
        if (state == 2) {
            SignIn signIn = signInMapper.selectCurrentState(userId);
            if (signIn.getIntegral() - totalIntegral < 0) {
                throw new RuntimeException("抱歉,您的积分不足");
            } else {
                // 需要参数: 用户id, 商品id, 订单消耗积分(页面传递)
                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                user.setId(userId);
                order.setUser(user);
                order.setTotalIntegral(totalIntegral);
                order.setSn(uuid);
                order.setState(state);
                orderMapper.insert(order);
                // 维护中间表
                detail.setGoodsId(goodsId);
                detail.setOrderId(order.getId());
                orderDetailMapper.insert(detail);
                // 减去用户积分
                signIn.setIntegral(signIn.getIntegral() - totalIntegral);
                signInMapper.updateByPrimaryKey(signIn);
            }
        } else {
            // 加入购物车
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            user.setId(userId);
            order.setUser(user);
            order.setTotalIntegral(totalIntegral);
            order.setSn(uuid);
            order.setState(state);
            orderMapper.insert(order);
            // 维护中间表
            detail.setGoodsId(goodsId);
            detail.setOrderId(order.getId());
            orderDetailMapper.insert(detail);
        }
    }

    /**
     * 通过用户id查询状态为已加购的订单
     *
     * @param userId 用户id
     * @param state  已加购
     * @return 订单
     */
    @Override
    public List<Order> getOrderByUserId(Long userId, Integer state) {
        return orderMapper.getOrderByUserId(userId, state);
    }

    /**
     * 结算购物车,批量更新状态
     */
    @Override
    public void updateBatch(Long[] ids, Integer state, Integer totalIntegral, Long userId) {
        SignIn signIn = signInMapper.selectCurrentState(userId);
        if (signIn.getIntegral() < totalIntegral) {
            throw new RuntimeException("抱歉,您的积分不足");
        } else {
            // 批量更新订单状态
            orderMapper.updateBatch(ids, state);
            // 减去用户积分
            signIn.setIntegral(signIn.getIntegral() - totalIntegral);
            signInMapper.updateByPrimaryKey(signIn);
        }
    }

    /**
     * 删除购物车订单
     *
     * @param id 订单id
     */
    @Override
    public void deleteOrder(Long id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    /**
     * 通过订单id, 修改订单总积分
     *
     * @param order x
     */
    @Override
    public void updateTotalIntegralById(Order order) {
        orderMapper.updateTotalIntegralById(order);
    }
}
